﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;

namespace Jumpton.SFA.Server.BusinessRule.MasterData
{
    public class ManufacturersManager:BusinessLogic
    {
        public MANUFACTURERS Load(int ID)
        {
            return (MANUFACTURERS)ObjectRow.Load(typeof(MANUFACTURERS), string.Format("this.ID={0}", ID));
        }

        public MANUFACTURERS LoadID(string Code)
        {
            return (MANUFACTURERS)ObjectRow.Load(typeof(MANUFACTURERS), string.Format("this.ManuCode='{0}'", Code));
        }

        public MANUFACTURERS Load(string code, decimal domainID, decimal ID)
        {
            return (MANUFACTURERS)ObjectRow.Load(typeof(MANUFACTURERS), string.Format("this.ManuCode='{0}' and this.DOMAINID={1} and this.ID<>'{2}'", code, domainID, ID));
        }


        public MANUFACTURERS LoadName(string Name, decimal DomainID)
        {
            return (MANUFACTURERS)ObjectRow.Load(typeof(MANUFACTURERS), string.Format("this.ManuName='{0}' AND this.DOMAINID={1}", Name, DomainID));
        }

        public MANUFACTURERS LoadDomianID(string Code, decimal DomainID)
        {
            return (MANUFACTURERS)ObjectRow.Load(typeof(MANUFACTURERS), string.Format("this.ManuCode='{0}' AND this.DOMAINID={1}", Code, DomainID));
        }

        public ObjectTable GetManufacturersTable(decimal damainid)
        {
            return new ObjectTable(typeof(MANUFACTURERS), "this.DOMAINID='" + damainid + "' and this.ACTIVE = '1'");
        }

        public ObjectTable GetManufacturersTable(decimal damainid,Boolean? active)
        {
            String Condition = "this.DOMAINID='" + damainid + "'";
            if(active != null)
                Condition+= " and this.ACTIVE = '" + (active.Value ? "1" : "0" )+ "'";
            return new ObjectTable(typeof(MANUFACTURERS), Condition);
        }

        public static ObjectTable GetVALIDYes(int Valid, decimal domainid)
        {
            StringBuilder sb = new StringBuilder();
            if (Valid >= 0)
            {
                sb.AppendFormat("this.ACTIVE='{0}'", Valid);
            }
            if (domainid > 0)
            {
                sb.AppendFormat("and this.DOMAINID='{0}'", domainid);
            }
            return new ObjectTable(typeof(MANUFACTURERS), sb.ToString().TrimStart('a', 'n', 'd'));
        }

        public static ObjectTable GetManus(decimal domainid)
        {
            return new ObjectTable(typeof(MANUFACTURERS), "this.DOMAINID='" + domainid + "' and this.ACTIVE = '1' ");
        }

        public static ObjectTable GetManus(decimal domainid, bool? CONTENDER)
        {
            string str = string.Format( "this.DOMAINID='" + domainid + "' and this.ACTIVE = '1' {0}",CONTENDER!= null? (" and this.CONTENDER=" + (CONTENDER.Value ? 1 :0)) : null);
            return new ObjectTable(typeof(MANUFACTURERS),str);
        }

        /// <summary>
        /// 保存制造商表信息
        /// </summary>
        /// <param name="city"></param>
        /// <returns></returns>
        public bool Save(MANUFACTURERS manu)
        {
            try
            {
                if (this.Load(manu.ManuCode, manu.DOMAINID, manu.ID) != null)
                {
                    throw new Exception("代码已存在");
                }

               
                
                manu.Save();
                
                return true;
            }
            catch (Exception ex)
            {
                this.message = ex.Message;
                throw ex;
            }
        }
    }
}
